package com.fafa.swing.base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.net.URL;

/**
 * @author : fafa
 * @version V1.0
 * @Project: SwingTest
 * @Package com.fafa.swing
 * @Description: TabbedPaneDemo
 * @date Date : 2019年11月26日 22:47
 */
public class TabbedPaneDemo extends JPanel {
    public static void main(String[] args) {
        JFrame frame = new JFrame("我的电脑--属性");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new TabbedPaneDemo(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }

    private TabbedPaneDemo(){
        super(new GridLayout(1,1));
        JTabbedPane tabbedPane = new JTabbedPane();
        ImageIcon icon = createImageIcon("tab.jpg");
        JComponent panel1 = makeTextPanel("计算机名");
        tabbedPane.addTab("计算机名",icon, panel1, "Does nothing");
        tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
        panel1 = makeTextPanel("硬件");
        tabbedPane.addTab("硬件",icon, panel1, "Does nothing");
        tabbedPane.setMnemonicAt(1, KeyEvent.VK_2);
        panel1 = makeTextPanel("高级");
        tabbedPane.addTab("高级",icon, panel1, "Does nothing");
        tabbedPane.setMnemonicAt(2, KeyEvent.VK_3);
        panel1 = makeTextPanel("系统保护");
        tabbedPane.addTab("系统保护",icon, panel1, "Does nothing");
        tabbedPane.setMnemonicAt(3, KeyEvent.VK_4);
        add(tabbedPane);
    }

    protected JComponent makeTextPanel(String text){
        JPanel panel = new JPanel(false);
        JLabel label = new JLabel(text);
        label.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1,1));
        panel.add(label);
        return panel;
    }
    protected static ImageIcon createImageIcon(String path){
        URL imgUrl = TabbedPaneDemo.class.getResource(path);
        if(imgUrl != null){
            return new ImageIcon(imgUrl);
        }else{
            System.err.println("Couldn't find file:"+path);
            return null;
        }
    }
}
